//
//  ConfirmViewController.m
//  yunyan
//
//  Created by haichuang on 2018/2/11.
//  Copyright © 2018年 haichuang. All rights reserved.
//

#import "ConfirmViewController.h"
#import "MainTabBarController.h"
@interface ConfirmViewController ()

@property (weak, nonatomic) IBOutlet UITextField *importNewPassword;

@property (weak, nonatomic) IBOutlet UIButton *completeBtn;
@property (weak, nonatomic) IBOutlet UITextField *confirePassWord;
@end

@implementation ConfirmViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = RGBA(240, 242, 246, 1);
    
        if (_isLogin) {
            
            if (_isRegister) {
 
            [self customNaviItemTitle:@"设置密码" isLeft:YES];
             }else{
            
              [self customNaviItemTitle:@"重置密码" isLeft:YES];
             }
        }else{
            
             [self customNaviItemTitle:@"设置密码" isLeft:YES];
        }
         
}

- (IBAction)completeBtn:(UIButton *)sender {
    
    
    if (_isLogin) {
        
     if (_isRegister) {
        
        if (self.importNewPassword.text==nil||self.importNewPassword.text.length
            ==0) {
            SHuiMBhudTextTimeStr(@"请输入新密码");
            return;
        }
        if (self.confirePassWord.text==nil||self.confirePassWord.text.length
                ==0) {
            SHuiMBhudTextTimeStr(@"请输入确认密码");
            return;
        }
         if (self.importNewPassword.text.length<6) {
             
             SHuiMBhudTextTimeStr(@"密码长度不能小于6");
             return;
         }
         if (self.confirePassWord.text.length<6) {
             
             SHuiMBhudTextTimeStr(@"密码长度不能小于6");
             return;
         }
        
        if (self.importNewPassword.text!= self.confirePassWord.text) {
            
            SHuiMBhudTextTimeStr(@"两次密码不一致");
            return;
        }
         if (_isPhone) {
              [self phoneRegisterRequest];
         }else{
             
             [self thirdSetPassword]; // 三方注册
         }
 
    }else{
        
        [self customNaviItemTitle:@"重置密码" isLeft:YES];
        
        if (self.importNewPassword.text==nil||self.importNewPassword.text.length
            ==0) {
            SHuiMBhudTextTimeStr(@"请输入新密码");
            return;
        }
        if (self.confirePassWord.text==nil||self.confirePassWord.text.length
            ==0) {
            SHuiMBhudTextTimeStr(@"请输入确认密码");
            return;
        }
        if (self.importNewPassword.text.length<6) {
            
            SHuiMBhudTextTimeStr(@"密码长度不能小于6");
            return;
        }
        if (self.confirePassWord.text.length<6) {
            
            SHuiMBhudTextTimeStr(@"密码长度不能小于6");
            return;
        }
        if (self.importNewPassword.text!= self.confirePassWord.text) {
            
            SHuiMBhudTextTimeStr(@"两次密码不一致");
            return;
        }
        [self changePhonePasswordRequest];
    }
    }else {
        
         [self customNaviItemTitle:@"设置密码" isLeft:YES];
        if (self.importNewPassword.text==nil||self.importNewPassword.text.length
            ==0) {
            SHuiMBhudTextTimeStr(@"请输入新密码");
            return;
        }
        if (self.confirePassWord.text==nil||self.confirePassWord.text.length
            ==0) {
            SHuiMBhudTextTimeStr(@"请输入确认密码");
            return;
        }
        if (self.importNewPassword.text.length<6) {
            
            SHuiMBhudTextTimeStr(@"密码长度不能小于6");
            return;
        }
        if (self.confirePassWord.text.length<6) {
            
            SHuiMBhudTextTimeStr(@"密码长度不能小于6");
            return;
        }
        if (self.importNewPassword.text!= self.confirePassWord.text) {
            
            SHuiMBhudTextTimeStr(@"两次密码不一致");
            return;
        }
        [self thirdSetPassword];
    }
//        MainTabBarController * root = [[MainTabBarController alloc]init];
//        [UIApplication sharedApplication].keyWindow.rootViewController = root;
   
}


-(void)phoneRegisterRequest{
    
    NSString * signMd5 = [SHJudgeTools md532BitLower:[NSString stringWithFormat:@"%@%@%@%@zyWf*y3vIjYxvRfH",[SHJudgeTools getDeviceId],self.mobileStr,self.codeStr,self.importNewPassword.text]];
    
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@",address_url,phone_reg_api];
    NSDictionary * parameter = @{@"mobile":self.mobileStr,
                                 @"password":self.importNewPassword.text,
                                 @"client":@"200",
                                 @"sign":signMd5,
                                 @"login_type":@"160",
                                 @"mac_code":[SHJudgeTools getDeviceId],
                                 @"code":self.codeStr
                                 };
    
    [PostNetWorking postNetWorkingStr:parametersStr  andparameters:parameter  Success:^( id  responseObject) {
        SHuiMBHidleYes;
        
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW , (1 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{
                
                
                LoginViewController * login = [[LoginViewController alloc]init];
                UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:login];
            
                [UIApplication sharedApplication].keyWindow.rootViewController = nav;
            });
        });

    } failed:^(NSURLSessionDataTask * _Nullable task,NSError *error) {
        
        NSHTTPURLResponse * responses = (NSHTTPURLResponse *)task.response;
        if (responses.statusCode == 404||responses.statusCode == 403) {
            SHuiMBhudTextTimeStr(@"参数内容不正确");
        }else {
            SHuiMBhudTextNetError;
        }
    }];
    
}

-(void)changePhonePasswordRequest{
    
    NSString * signMd5 = [SHJudgeTools md532BitLower:[NSString stringWithFormat:@"%@%@%@zyWf*y3vIjYxvRfH",self.mobileStr,self.importNewPassword.text,self.codeStr]];
    
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@",address_url,change_password_api];
    NSDictionary * parameter = @{@"mobile":self.mobileStr,
                                 @"password":self.importNewPassword.text,
                                 @"sign":signMd5,
                                 @"code":self.codeStr
                                 };
    
    [PostNetWorking postNetWorkingStr:parametersStr  andparameters:parameter  Success:^( id  responseObject) {
        SHuiMBHidleYes;
        
        SHuiMBhudTextTimeStr(@"设置成功");
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW , (1 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{
                
                
                LoginViewController * login = [[LoginViewController alloc]init];
                UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:login];
                
                [UIApplication sharedApplication].keyWindow.rootViewController = nav;
            });
        });
        
    } failed:^(NSURLSessionDataTask * _Nullable task,NSError *error) {
        
        NSHTTPURLResponse * responses = (NSHTTPURLResponse *)task.response;
        
        if (responses.statusCode == 404||responses.statusCode == 403) {
            SHuiMBhudTextTimeStr(@"参数内容不正确");
        }else if (responses.statusCode == 502){
            SHuiMBhudTextTimeStr(@"不能与原密码相同");
        }else {
            SHuiMBhudTextNetError;
        }
    }];
    
}


-(void)thirdSetPassword{
    

    NSString *  signMd5 = [SHJudgeTools md532BitLower:[NSString stringWithFormat:@"%@%@%@%@%@%@%@%@zyWf*y3vIjYxvRfH",[SHJudgeTools getDeviceId],self.mobileStr,self.codeStr,USER_DEFAULTGet(@"login_type"),USER_DEFAULTGet(@"openid"),USER_DEFAULTGet(@"access_token"),USER_DEFAULTGet(@"unionid"),self.importNewPassword.text]];
    
    
    NSDictionary *  dic = @{@"access_token": USER_DEFAULTGet(@"access_token"),
                            @"mac_code":[SHJudgeTools getDeviceId],
                            @"login_type":USER_DEFAULTGet(@"login_type"),
                            @"unionid": USER_DEFAULTGet(@"unionid"),
                            @"client":@"200",
                            @"openid": USER_DEFAULTGet(@"openid"),
                            @"sign":signMd5,
                            @"mobile":self.mobileStr,
                            @"code":self.codeStr,
                            @"password":self.importNewPassword.text
                            };
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(third_reg_api) andparameters:dic Success:^(id responseObject) {
        
        SHuiMBhudTextTimeStr(@"设置成功");
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            
            USER_DEFAULTSet(responseObject[@"info"][@"user_id"], @"user_id");
            USER_DEFAULTSet(responseObject[@"info"][@"user_nick"], @"user_nick");
            USER_DEFAULTSet(responseObject[@"info"][@"mobile"], @"mobile");
            USER_DEFAULTSet(responseObject[@"info"][@"head_url"], @"head_url");
            USER_DEFAULTSet(responseObject[@"info"][@"birthday"], @"birthday");
            USER_DEFAULTSet(responseObject[@"info"][@"user_sex"], @"user_sex");
            USER_DEFAULTSet(responseObject[@"token"], @"token");
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW , (1 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{
                
                MainTabBarController * root = [[MainTabBarController alloc]init];
                [UIApplication sharedApplication].keyWindow.rootViewController = root;
            });
        });
        
    } failed:^(NSURLSessionDataTask * _Nullable task, NSError *error) {
        
        NSHTTPURLResponse * responses = (NSHTTPURLResponse *)task.response;
        if (responses.statusCode == 404||responses.statusCode == 403) {
            SHuiMBhudTextTimeStr(@"参数内容不正确");
        }else {
            SHuiMBhudTextNetError;
        }
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
